﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Windows.Forms;

namespace Dusk_Tile_Editor
{
    public class GraphicsPanel : Panel
    {
        SGP.ManagedDirect3D D3D = SGP.ManagedDirect3D.Instance;
        SGP.ManagedTextureManager MTM = SGP.ManagedTextureManager.Instance;

        public TileSet tileSet = new TileSet();

        public Point selectedImageTile;

        public Point[,] Layer1 = new Point[10, 10];
        public Point[,] Layer2 = new Point[10, 10];
        public Point[,] Layer3 = new Point[10, 10];

        public List<Tile> Tiles = new List<Tile>();

        public List<SpawnPoint> SpawnPoints = new List<SpawnPoint>();

        public List<CameraPoint> CameraPoints = new List<CameraPoint>();

        public int imageID;

        public Rectangle selectedRectangle;

        public Size mapSize;

        public short Layer = 0;

        public void SetMapSize( int newWidth, int newHeight)
        {
            if (newWidth != 0)
            {
                mapSize.Width = newWidth;
            }
            if (newHeight != 0)
            {
                mapSize.Height = newHeight;
            }
            this.AutoScrollMinSize = new Size(mapSize.Width * tileSet.TileSize.Width, mapSize.Height * tileSet.TileSize.Height);
        }

        public GraphicsPanel()
        {
            this.DoubleBuffered = true;    
        }

        public void Initialize( TileSet newtileSet)
        {
            this.AutoScrollMinSize = new Size(400, 400);
            this.tileSet = newtileSet;
        }

        public void UpdateTileSet(TileSet newtileSet)
        {
            this.tileSet = newtileSet;
        }

        public void Render()
        {
            D3D.ChangeDisplayParam(this);
            Point offset = new Point(0, 0);
            offset.X += this.AutoScrollPosition.X;
            offset.Y += this.AutoScrollPosition.Y;

            D3D.Clear(this, Color.Black);
            D3D.DeviceBegin();
            D3D.SpriteBegin();


                int leftSide = -offset.X / tileSet.TileSize.Width;
                int topSide = -offset.Y / tileSet.TileSize.Height;
                int rightSide = (this.Width - offset.X) / tileSet.TileSize.Width;
                int bottomSide = (this.Height - offset.Y) / tileSet.TileSize.Height;
                if( rightSide >= mapSize.Width)
                    rightSide = mapSize.Width - 1;

                if( bottomSide >= mapSize.Height)
                    bottomSide = mapSize.Height - 1;

                for ( leftSide = -offset.X / tileSet.TileSize.Width; leftSide <= rightSide; leftSide++)
                {
                    for (topSide = -offset.Y / tileSet.TileSize.Height; topSide <= bottomSide; topSide++)
                    {
                        if (Layer1[leftSide, topSide].X < tileSet.TileSetSize.Width && Layer1[leftSide, topSide].Y < tileSet.TileSetSize.Height)
                                MTM.Draw(imageID, leftSide * tileSet.TileSize.Width + offset.X, topSide * tileSet.TileSize.Height + offset.Y,
                                    1.0f, 1.0f, new Rectangle(Layer1[leftSide, topSide].X * tileSet.TileSize.Width,
                                    Layer1[leftSide, topSide].Y * tileSet.TileSize.Height, tileSet.TileSize.Width,
                                    tileSet.TileSize.Height), 0, 0, 0.0f, 0);
                            if (Layer == 1 || Layer == 2 )
                            {
                                if( Layer2[leftSide, topSide].X < tileSet.TileSetSize.Width && Layer2[leftSide, topSide].Y < tileSet.TileSetSize.Height)
                                MTM.Draw(imageID, leftSide * tileSet.TileSize.Width + offset.X, topSide * tileSet.TileSize.Height + offset.Y,
                                        1.0f, 1.0f, new Rectangle(Layer2[leftSide, topSide].X * tileSet.TileSize.Width,
                                        Layer2[leftSide, topSide].Y * tileSet.TileSize.Height, tileSet.TileSize.Width,
                                        tileSet.TileSize.Height), 0, 0, 0.0f, 0);
                            }
                            if (Layer == 2 )
                            {
                                if( Layer3[leftSide, topSide].X < tileSet.TileSetSize.Width && Layer3[leftSide, topSide].Y < tileSet.TileSetSize.Height )
                                MTM.Draw(imageID, leftSide * tileSet.TileSize.Width + offset.X, topSide * tileSet.TileSize.Height + offset.Y,
                                        1.0f, 1.0f, new Rectangle(Layer3[leftSide, topSide].X * tileSet.TileSize.Width,
                                        Layer3[leftSide, topSide].Y * tileSet.TileSize.Height, tileSet.TileSize.Width,
                                        tileSet.TileSize.Height), 0, 0, 0.0f, 0);
                            }
                    }

                }

            D3D.Sprite.Flush();

            for (leftSide = -offset.X / tileSet.TileSize.Width; leftSide <= rightSide; leftSide++)
            {
                for (topSide = -offset.Y / tileSet.TileSize.Height; topSide <= bottomSide; topSide++)
                {
                    D3D.DrawEmptyRect(new Rectangle(leftSide * tileSet.TileSize.Width + offset.X,
                            topSide * tileSet.TileSize.Height + offset.Y, tileSet.TileSize.Width,
                            tileSet.TileSize.Height), Color.Black);
                }
            }

            for (int i = 0; i < CameraPoints.Count; i++)
            {
                D3D.DrawRect(new Rectangle(CameraPoints[i].cameraPoint.X + offset.X, CameraPoints[i].cameraPoint.Y + offset.Y,
                    10, 10), Color.White);
            }


            for (int i = 0; i < SpawnPoints.Count; i++)
            {
                switch (SpawnPoints[i].eEnemySpawn)
                {
                    case SpawnPoint.EnemySpawnType.SPAWN_PLAYER:
                        {
                            D3D.DrawRect(new Rectangle(SpawnPoints[i].WorldPos.X + offset.X, SpawnPoints[i].WorldPos.Y + offset.Y,
                            10, 10), Color.Green);
                        }break;
                    case SpawnPoint.EnemySpawnType.SPAWN_BOSS_BOMBRO:
                        {
                            D3D.DrawRect(new Rectangle(SpawnPoints[i].WorldPos.X + offset.X, SpawnPoints[i].WorldPos.Y + offset.Y,
                            10, 10), Color.Red);
                        } break;
                    case SpawnPoint.EnemySpawnType.SPAWN_BOSS_ROCKGOLEM:
                        {
                            D3D.DrawRect(new Rectangle(SpawnPoints[i].WorldPos.X + offset.X, SpawnPoints[i].WorldPos.Y + offset.Y,
                            10, 10), Color.Red);
                        } break;
                    case SpawnPoint.EnemySpawnType.SPAWN_BOSS_SPEEDREAVER:
                        {
                            D3D.DrawRect(new Rectangle(SpawnPoints[i].WorldPos.X + offset.X, SpawnPoints[i].WorldPos.Y + offset.Y,
                            10, 10), Color.Red);
                        } break;
                    case SpawnPoint.EnemySpawnType.SPAWN_BOSS_RIOTOFFICER:
                        {
                            D3D.DrawRect(new Rectangle(SpawnPoints[i].WorldPos.X + offset.X, SpawnPoints[i].WorldPos.Y + offset.Y,
                            10, 10), Color.Red);
                        } break;
                    case SpawnPoint.EnemySpawnType.SPAWN_PEON:
                        {
                            D3D.DrawRect(new Rectangle(SpawnPoints[i].WorldPos.X + offset.X, SpawnPoints[i].WorldPos.Y + offset.Y,
                            10, 10), Color.Blue);
                        } break;
                    case SpawnPoint.EnemySpawnType.SPAWN_NINJA:
                        {
                            D3D.DrawRect(new Rectangle(SpawnPoints[i].WorldPos.X + offset.X, SpawnPoints[i].WorldPos.Y + offset.Y,
                            10, 10), Color.Yellow);
                        } break;
                    case SpawnPoint.EnemySpawnType.SPAWN_SHIELDED:
                        {
                            D3D.DrawRect(new Rectangle(SpawnPoints[i].WorldPos.X + offset.X, SpawnPoints[i].WorldPos.Y + offset.Y,
                            10, 10), Color.Violet);
                        } break;
                    case SpawnPoint.EnemySpawnType.SPAWN_SNIPER:
                        {
                            D3D.DrawRect(new Rectangle(SpawnPoints[i].WorldPos.X + offset.X, SpawnPoints[i].WorldPos.Y + offset.Y,
                            10, 10), Color.Orange);
                        } break;
                }
                
            }
            D3D.DrawEmptyRect(new Rectangle(selectedRectangle.X,selectedRectangle.Y, tileSet.TileSize.Width, tileSet.TileSize.Height ), Color.Red);

            

            D3D.SpriteEnd();
            D3D.DeviceEnd();
            D3D.Present(this);
        }
    }
}
